package com.java.restaurant.waiter.actions.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.java.restaurant.generic.db.RestuarantDBConnection;
import com.java.restaurant.waiter.beans.User;

public class LoginDao {

	public User verifyLogin(String userName, String password) throws Exception {

		User user = null;
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;

		String sql = "SELECT ID, name, username FROM hotel.login WHERE ";
		sql += " username = ? AND password = ?";

		try {
			conn = RestuarantDBConnection.getConnection();

			ps = conn.prepareStatement(sql);

			ps.setString(1, userName);
			ps.setString(2, password);
			rs = ps.executeQuery();

			while (rs.next()) {
				user = new User();

				user.setId(rs.getLong("ID"));
				user.setUserName(rs.getString("username"));
				user.setName(rs.getString("name"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return user;

	}

}
